var api = require('./commapi.js');

var DIRCONSTANT = 
{
	//-文件数据库规模-
	FILESCALE_MINI	:	0,	//微型(同一目录下1000个文件)
	FILESCALE_SMALL	:	1,	//小型(一级子目录，每个目录100文件，支持100*100=1w个文件)
	FILESCALE_MID	:	2,	//中型(二级子目录，每个目录100文件，支持100*100*100=100w个文件)

	//-文件记录-
	DIRFLD_INDEXID	:	0,	//在数组中对应id
	DIRFLD_LEVEL	:	1,	//当前的目录级别
	DIRFLD_NAME		:	2,	//目录名
	DIRFLD_COUNT	:	3,	//当前下级个数
	DIRFLD_NEXTID	:	4,	//下一个开始的id号
	DIRFLD_PARENTID	:	5,	//父id

	version:	"20111228",
	author:		"mr.7"	
};

var	dir = 
{
	//-设置数据库规模-
	setDbScale : function(n)
	{
		switch(n)
		{
			case DIRCONSTANT.FILESCALE_MINI:
			{
				this._dirLevelCount = 0;
				this._dirMaxCount = 0;
				this._fileMaxCount = 1000;
				break;
			}

			case DIRCONSTANT.FILESCALE_SMALL:
			{
				this._dirLevelCount = 1;
				this._dirMaxCount = 100;
				this._fileMaxCount = 100;
				break;
			}

			case DIRCONSTANT.FILESCALE_MID:
			{
				this._dirLevelCount = 2;
				this._dirMaxCount = 100;
				this._fileMaxCount = 100;
				break;
			}
		}
	},

	//-打开-
	open : function(path)
	{
		this._strDBDir = path;
		this._strDBFile= path + "/dir.db";

		//-打开文件库-
		this._dirMap = require('./map.js').Map;		
		_openFile(this);

		function _openFile(obj)
		{
			var data = api.loadFile(obj._strDBFile);
			if(data=="")
				return;
			var arr = data.split(/\r\n/);
			if(arr.length<=0)
				return;

			var	rcd;
			//-第一行是设置信息-
			rcd = arr[0].split("||");
			if(rcd.length>1)
			{
				obj._dirLevelCount = parseInt(rcd[0]);
				obj._dirMaxCount = parseInt(rcd[1]);
				obj._fileMaxCount = parseInt(rcd[2]);
			}
			//后继的是记录信息
			for(var i=1; i<arr.length; i++)
			{
				rcd = arr[i].split("||");
				rcd[DIRCONSTANT.DIRFLD_INDEXID] = parseInt(rcd[DIRCONSTANT.DIRFLD_INDEXID]);
				rcd[DIRCONSTANT.DIRFLD_LEVEL] = parseInt(rcd[DIRCONSTANT.DIRFLD_LEVEL]);
				rcd[DIRCONSTANT.DIRFLD_COUNT] = parseInt(rcd[DIRCONSTANT.DIRFLD_COUNT]);
				rcd[DIRCONSTANT.DIRFLD_NEXTID] = parseInt(rcd[DIRCONSTANT.DIRFLD_NEXTID]);
				rcd[DIRCONSTANT.DIRFLD_PARENTID] = parseInt(rcd[DIRCONSTANT.DIRFLD_PARENTID]);
				obj._dirRst.push(rcd);
				obj._dirMap.put(rcd[DIRCONSTANT.DIRFLD_NAME], obj._dirRst.length-1);
			}
		}
	},
	
	//-保存 -	
	save : function()
	{		
		var data = [this._dirLevelCount, this._dirMaxCount, this._fileMaxCount].join("||");
		var i,rcd;
		for(i=0; i<this._dirRst.length; i++)
		{
			rcd = this._dirRst[i];
			
			//-保存当前记录-
			if(data!="")
				data +="\r\n";
			data += rcd.join("||");
		}
		
		//-保存文件-
		api.saveFile(this._strDBFile, data);
	},

	//-关闭-	
	close : function()
	{
		if(this._dirMap)
			this._dirMap.clear();

		this._dirRst.length = 0;

		this._strDBDir = "";
		this._strDBFile = "";
	},

	//-获取目录对应的记录信息-
	fGet : function(strPath)
	{
		var rcddir;
		var value = this._dirMap.get(strPath);
		if(value)
			rcddir = this._dirRst[value];

		return rcddir;
	},

	//-获取某个id对应子目录名称，可以多级-
	fGetPath : function(id, rcdParent)
	{
		var rtn = "";
		if(rcdParent!=undefined)
			rtn = rcdParent[DIRCONSTANT.DIRFLD_NAME];
		if(rtn!="")
			rtn += "/";			
		var tmp = 1000+id;
		rtn += tmp.toString().substr(1);		
		return rtn;
	},

	//-创建指定目录下的子目录-
	//	strPath 不能带 ./
	fCreateDir : function(strPath, rcdParent)
	{
		//-创建目录-
		if(strPath!=".")
		{
			var curpath = this._strDBDir;			
			//-如果是子目录,去除前缀./-
			if(rcdParent && 0!=rcdParent[DIRCONSTANT.DIRFLD_LEVEL])
				curpath += rcdParent[DIRCONSTANT.DIRFLD_NAME].substr(1);
			api.createDir(curpath, strPath);
		}
		
		//-添加该目录索引信息-
		var strKey;
		if(strPath!="." && rcdParent)		
			strKey = rcdParent[DIRCONSTANT.DIRFLD_NAME] + "/" + strPath;
		else
			strKey = strPath;

		var rcdnew = [	-1,
						rcdParent ? rcdParent[DIRCONSTANT.DIRFLD_LEVEL]+1:0, 
						strKey, 
						0, 
						0, 
						rcdParent ? rcdParent[DIRCONSTANT.DIRFLD_INDEXID]:-1
					];
		this._dirRst.push(rcdnew);
		this._dirMap.put(strKey, this._dirRst.length-1);

		//-调整最后一个index-
		function fAdjustIndex(rst)
		{
			if(rst.length>0)
			{
				rst[rst.length-1][DIRCONSTANT.DIRFLD_INDEXID]=rst.length-1;
			}
		}
		fAdjustIndex(this._dirRst);

		//-创建目录后，修改父目录信息-
		if(rcdParent)
		{
			rcdParent[DIRCONSTANT.DIRFLD_NEXTID]+= 1;
			rcdParent[DIRCONSTANT.DIRFLD_COUNT] += 1;
		}

		return this._dirRst[this._dirRst.length-1];
	},

	//-处理根目录信息-
	fNewDir0 : function()
	{
		var rtn =	this._dirRst.length<=0 ? 
					this.fCreateDir(".") : 
					this._dirRst[0];
		return rtn;
	},

	//-申请一级目录-
	fNewDir1 : function()
	{
		//-获取根目录,如果不存在，生成根目录信息-
		var rcdroot = this.fNewDir0();

		//-如果一级子目录不存在,创建之-
		var strCurPath;
		if(rcdroot[DIRCONSTANT.DIRFLD_COUNT]<=0)
		{
			strCurPath = this.fGetPath(0);
			return this.fCreateDir(strCurPath, rcdroot);
		}

		//-获取当前目录情况-
		strCurPath = this.fGetPath(rcdroot[DIRCONSTANT.DIRFLD_NEXTID]-1, rcdroot);
		var rcdcur = this.fGet(strCurPath);
		if(rcdcur===undefined)
		{
			return this.fCreateDir(strCurPath, rcdroot);
		}
		else
		{
			if(rcdcur[DIRCONSTANT.DIRFLD_COUNT]<this._fileMaxCount)
				return rcdcur;
			else
			{
				strCurPath = this.fGetPath(rcdroot[DIRCONSTANT.DIRFLD_NEXTID]);				
				return this.fCreateDir(strCurPath, rcdroot);
			}
		}
	},

	//-申请二级目录-
	fNewDir2 : function()
	{
		//-获取根目录,如果不存在，生成根目录信息-
		var rcdroot = this.fNewDir0();

		//-如果第一个目录不存在,循环创建之-
		var strCurPath;
		var rcdfirst;
		if(rcdroot[DIRCONSTANT.DIRFLD_COUNT]<=0)
		{
			strCurPath = this.fGetPath(0);
			rcdfirst = this.fCreateDir(strCurPath, rcdroot);
			return this.fCreateDir(strCurPath, rcdfirst);
		}

		//-获取最后一个目录情况-
		var rcdRtn;
		var	rcdLast = this._dirRst[this._dirRst.length-1];
		switch(rcdLast[DIRCONSTANT.DIRFLD_LEVEL])
		{
			case 1:	//-如果是一级目录,需要创建缺省的第一个二级目录-
			{
				strCurPath = this.fGetPath(0);
				rcdRtn = this.fCreateDir(strCurPath, rcdLast);
				break;
			}

			case 2:	//-如果是二级目录
			{
				//-二级目录下还可以放文件-
				if(rcdLast[DIRCONSTANT.DIRFLD_COUNT]<this._fileMaxCount)
				{					
					rcdRtn = rcdLast;
				}
				else//-二级目录下已满，新开一个二级目录-
				{
						//-该一级目录下有空，直接新开二级目录-
					var rcdParent = this._dirRst[rcdLast[DIRCONSTANT.DIRFLD_PARENTID]];
					if(rcdParent[DIRCONSTANT.DIRFLD_COUNT]<this._dirMaxCount)
					{
						strCurPath = this.fGetPath(rcdParent[DIRCONSTANT.DIRFLD_NEXTID]);
						rcdRtn = this.fCreateDir(strCurPath, rcdParent);
					}
					else
					{
						//-该一级目录也满，再开一级目录和二级目录-
						strCurPath = this.fGetPath(rcdroot[DIRCONSTANT.DIRFLD_NEXTID]);
						rcdParent = this.fCreateDir(strCurPath, rcdroot);

						strCurPath = this.fGetPath(0);
						rcdRtn = this.fCreateDir(strCurPath, rcdParent);
					}					
				}
				break;
			}
		}

		return rcdRtn;
	},

	//-申请目录-
	fNewDir : function()
	{
		var rtn = null;
		switch(this._dirLevelCount)
		{
			case 0:	break;

			case 1:
			{
				rtn = this.fNewDir1();
				break;
			}

			case 2:
			{
				rtn = this.fNewDir2();
				break;
			}
		}

		return rtn;
	},

	//-获取绝对路径-
	fGetNewPath:function(rcd, suffix)
	{
		var strPath =	rcd[DIRCONSTANT.DIRFLD_NAME] + "/" 
					  + this.fGetPath(rcd[DIRCONSTANT.DIRFLD_NEXTID])+"."+suffix;
		//var strLastPath = this._strDBDir + strPath.substr(1);
		var strLastPath = strPath;

		return strLastPath;
	},
	
	//-申请文件名-
	fNewFile : function(suffix)
	{
		//-获取所在子目录-
		var	rcd = this.fNewDir();
		var strPath ="";
		if(rcd)
		{
			strPath = this.fGetNewPath(rcd, suffix);

			rcd[DIRCONSTANT.DIRFLD_NEXTID] +=1; 
			rcd[DIRCONSTANT.DIRFLD_COUNT]  +=1;
		}
				
		return [rcd, strPath];
	},

	//-删除文件时更新目录信息-
	fDelFile : function(strDir)
	{
		var rcddir = this.fGet(strDir);
		if(!rcddir)
			return false;
		if(rcddir[DIRCONSTANT.DIRFLD_COUNT]>=0)
			rcddir[DIRCONSTANT.DIRFLD_COUNT]-=1;
		return true;
	},

	_dirMap	:	null,
	_dirRst	:	[],
		
	_dirLevelCount	:	2,		//最大目录级别数
	_dirMaxCount	:	60,		//最大子目录数
	_fileMaxCount	:	100,	//最大文件数
	
	_strDBDir: "",				//数据库根路径
	_strDBFile:"",				//数据库文件

	version:	"20111227",
	author:		"mr.7"
};

exports.dir = dir;